package com.study.lession.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import com.study.lession.contant.Constants;

/**
 * Servlet Filter implementation class SessionFilter
 */
@WebFilter("/ContactServlet")
public class SessionFilter implements Filter {

  
	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		System.out.println(req.getRequestURI());
		if(null == req.getSession().getAttribute(Constants.SESSION_KEY)) {
			req.setAttribute("msg", "会话已过期，请重新登录");
			req.getRequestDispatcher("LoginServlet").forward(req, response);
		} else {
			chain.doFilter(request, response);
		}
		
	}


}
